/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools;

import embeddedpaint.EmbeddedPaint;
import embeddedpaint.model.feature.AImageTool;
import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerManager;
import embeddedpaint.user_interface.tools.dialog.FrameRotatePicture;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class ToolsRotatePicture extends AImageTool implements MouseMotionListener, MouseListener, Observer
{
    private static final String M_LABEL_TEXT = "Rotate Layer";
    private static final String M_ICON_PATH  = "images/rotate.png";
    
    private FrameRotatePicture  m_frame;
    private HashMap             m_initRotate;
    private Point               m_from;

    public ToolsRotatePicture (LayerManager imageManager, JFrame application)
    {
        super(imageManager, application);

        this.m_initRotate = new HashMap();
    }

    @Override
    public boolean isToggleButton ()
    {
        return true;
    }

    @Override
    public String getLabel ()
    {
        return M_LABEL_TEXT;
    }

    @Override
    protected String getIconName ()
    {
        return M_ICON_PATH;
    }

    @Override
    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource() instanceof JToggleButton)
        {
            JToggleButton buton = (JToggleButton) e.getSource();

            if (buton.isSelected())
            {
                createObserver();
            }
            else
            {
                removeObservers();
            }
        }
    }

    public void createObserver ()
    {
        m_frame = new FrameRotatePicture(m_imageManager, m_application);
        m_frame.addObserver(this);

        ((EmbeddedPaint) this.m_application).getM_scrollPane().addMouseListener      (this);
        ((EmbeddedPaint) this.m_application).getM_scrollPane().addMouseMotionListener(this);

        this.m_actif = true;
    }

    public void removeObservers ()
    {
        this.m_actif = false;
        
        ((EmbeddedPaint) this.m_application).getM_scrollPane().removeMouseListener      (this);
        ((EmbeddedPaint) this.m_application).getM_scrollPane().removeMouseMotionListener(this);
        
        if (m_frame != null)
        {
            m_frame = null;
        }
        
        this.setChanged();
        this.notifyObservers();
    }

    @Override
    public void dispose ()
    {
        if (m_frame != null)
        {
            m_frame.removeObserver(this);
            m_frame.askApplyAndChange();
        }
        
        removeObservers();
    }

    @Override
    public void mouseDragged (MouseEvent e)
    {
        Point newPoint = e.getPoint();

        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            double arc1 = Math.atan2(this.m_from.x - img.getM_x(), this.m_from.y - img.getM_y());
            double arc2 = Math.atan2(newPoint.x - img.getM_x(), newPoint.y - img.getM_y());

            double angle = arc1 - arc2;

            m_frame.setDegrees(Math.toDegrees(angle) + ((double) this.m_initRotate.get(img)));
        }
    }

    @Override
    public void mouseMoved (MouseEvent e)
    {
    }

    @Override
    public void mouseClicked (MouseEvent e)
    {
    }

    @Override
    public void mousePressed (MouseEvent e)
    {
        this.m_from = e.getPoint();
        
        this.m_initRotate.clear();
        
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();

        for (ALayer img : selectedImages)
        {
            this.m_initRotate.put(img, img.getM_rotate());
        }
    }

    @Override
    public void mouseReleased (MouseEvent e)
    {
    }

    @Override
    public void mouseEntered (MouseEvent e)
    {
    }

    @Override
    public void mouseExited (MouseEvent e)
    {
    }

    @Override
    public void update (Observable o, Object arg)
    {
        this.removeObservers();
    }
}